<?php
//格式化表单输入值
function handledata($data)
{
    $data = trim($data);
    $data = strip_tags($data);
    $data = nl2br($data);
    $data = htmlspecialchars($data);
    return $data;
}

//判断post是否含有空值，是返回false,否返回true
function isnull($data){
    foreach ($data as $v){
        if (empty($v)) return false;
    }
    return true;
}

//加密密码
function enpwd($pwd,$salt){
    return md5(md5($pwd).md5($salt));
}

//检查Cookie是否有效
function checkCookie($con){
    if(!empty($_COOKIE['id'])||!empty($_COOKIE['key'])){
        $loginUserInfo=queryInfoByID($con,queryisNull,$_COOKIE['id']);
        if(!empty($loginUserInfo)&&$loginUserInfo[2]==$_COOKIE['key']){
            loginSuccess($_SERVER['REMOTE_ADDR'],$loginUserInfo);
        }else{
            $_SESSION['$errorTipArr'][]='登录信息失效';
            setcookie('id','',time()-1,'/');
            setcookie('key','',time()-1,'/');
            //用户已被删除或密码已失效
        }
    }
}

//拦截向未登录页面的请求 并 区分管理与用户
function filterLogout(){
    if (empty($_SESSION['token'])){
        header('location:/model/public/login.php');
    }
    if($_SESSION['loginIden']=='管理员'){
        if(substr_count($_SERVER['SCRIPT_NAME'],'/user/')>0){
            header('location:/model/admin/home.php');
        }
    }else{
        if(substr_count($_SERVER['SCRIPT_NAME'],'/admin/')>0){
            header('location:/model/user/home.php');
        }
    }
}

//生成用户头像背景颜色
function random_color(){
    mt_srand((double)microtime()*1000000);
    $c = '#';
    while(strlen($c)<6){
        $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return $c;
}

//分配令牌并跳转用户页面
function loginSuccess($tokenKey,$arr=[],$messArr=[]){
    session_start();
    $_SESSION['token']=md5($tokenKey);
    $_SESSION['loginId']=$arr[0];
    $_SESSION['loginName']=$arr[1];
    $_SESSION['loginPwd']=$arr[2];
    $_SESSION['loginSalt']=$arr[7];
    $_SESSION['loginSex']=usexTurn($arr[3]);
    $_SESSION['loginIden']=uidenTurn($arr[6]);
    $_SESSION['loginDesc']=$arr[5];
    $_SESSION['picolor']=random_color();
    $_SESSION['loginPic']=$arr[4];
    $_SESSION['lastLoginTime']=$arr[8];
    $_SESSION['loginUnReadMessInfo']=$messArr;
    if($arr[6]==0){
        header('location:../admin/home.php');
    }else{
        header('location:../user/home.php');
    }
}

//性别格式转换
function usexTurn($sex){
    switch ($sex){
        case 0:
            return '保密';
            break;
        case 1:
            return '男';
            break;
        case 2:
            return '女';
    }
}

//等级格式转换
function uidenTurn($iden){
    switch ($iden){
        case 0:
            return '管理员';
            break;
        case 1:
            return '青铜会员';
            break;
        case 2:
            return '白银会员';
            break;
        case 3:
            return '钻石会员';
            break;
        case 4:
            return '黑金会员';
            break;
        case 5:
            return '封禁中';
            break;
    }
}

//等级格式录入数据库转换
function uidenRevTurn($iden){
    switch ($iden){
        case '管理员':
            return 0;
            break;
        case '青铜会员':
            return 1;
            break;
        case '白银会员':
            return 2;
            break;
        case '钻石会员':
            return 3;
            break;
        case '黑金会员':
            return 4;
            break;
    }
}

//等级颜色转换
function idenBadge($iden){
    switch ($iden){
        case 0:
            return 'success';
            break;
        case 1:
            return 'light';
            break;
        case 2:
            return 'primary';
            break;
        case 3:
            return 'warning';
            break;
        case 4:
            return 'dark';
            break;
        case 5 :
            return 'danger';
            break;
    }
}

//时间转换
function timeDiff($time){
    if(empty($time)){
        return '萌新用户未曾登录';
    }else{
        $d1=strtotime(date("Y-m-d"));
        $d2=strtotime(substr($time,0,10));
        $diffDays=round(($d1-$d2)/3600/24);
        if($diffDays==0){
            return '几小时前';
        }elseif($diffDays>0 && $diffDays<=30){
            return $diffDays.'天前';
        }elseif($diffDays>30 && $diffDays<=360){
            return floor($diffDays/30).'个月前';
        }elseif($diffDays>360){
            return floor($diffDays/360).'年前';
        }
    }
}

//长文本格式限制
function textsub($text,$len){
    $str=str_replace("&lt;br /&gt;","",$text);
//$newtext=htmlspecialchars($str);
    $newtext=strip_tags($str);
    if(mb_strlen($newtext)>$len){
        return mb_substr($newtext,0,$len).'...';
    }else{
        return mb_substr($newtext,0,$len);
    }
}


//生成性别、等级下拉表单控件
function sexSelect($arr,$espe){
    $htmlStr='';
    foreach ($arr as $key=>$val){
        if($val==$espe){
            $htmlStr.='<option value='.$key.' selected>'.$val.'</option>';
        }else{
            $htmlStr.='<option value='.$key.' >'.$val.'</option>';
        }
    }
    return $htmlStr;
}

//随机文件名
function get_randomFileName($fileName,$length) {
    //获取文件名（不带后缀）
    $filename=pathinfo($fileName, PATHINFO_FILENAME);
    //获取文件后缀
    $filext=pathinfo($fileName, PATHINFO_EXTENSION);
    $min=pow(10,($length-1));
    $max=pow(10,$length)-1;
    return $filename.'_'.time().'_'.mt_rand($min,$max).'.'.$filext;
}

//计算页数
function pageFunc($rows){
    if ($rows % 3 == 0) {
        return $rows / 3;
    } else {
        return ceil($rows / 3);
    }
}